package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.BuyBookViewHolder;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.refresh.XRefreshView;
import com.jjwxc.jwjskandriod.framework.refresh.XRefreshViewFooter;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.model.UserSubResponse;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;

import java.util.ArrayList;
import java.util.List;

public class BuyBookDetailsActivity extends BaseActivity {

    private RecyclerView rv_buybook;
    private BuyBookAdapter adapter;
    private XRefreshView refreshView;
    public static long lastRefreshTime;
    private String bookId;

    private int page = 0;

    @Override
    public int getContentViewId() {
        return R.layout.activity_buybookdetails;
    }

    @Override
    public void findView() {
        refreshView = findViewById(R.id.buybook_view);
        rv_buybook = findViewById(R.id.rv_buybook);
    }

    @Override
    public void setListener() {
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                page = 0;
                listUserChapterSubscribe(page);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page += 1;
                listUserChapterSubscribe(page * 20);
            }
        });
    }

    @Override
    public void afterCreate() {
        String bookName = getIntent().getStringExtra("BOOK_NAME");
        setTitle(bookName);
        bookId = getIntent().getStringExtra("BOOK_ID");
        rv_buybook.setNestedScrollingEnabled(false);
        rv_buybook.setFocusable(false);
        rv_buybook.setLayoutManager(new LinearLayoutManager(BuyBookDetailsActivity.this));
        rv_buybook.addItemDecoration(new LinearLayouDecoration(BuyBookDetailsActivity.this,
                LinearLayouDecoration.HORIZONTAL, R.dimen.border8, R.color.transparent));
        rv_buybook.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new BuyBookAdapter(this);
        rv_buybook.setAdapter(adapter);

        refreshView.setPinnedTime(1000);
        refreshView.setMoveForHorizontal(true);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(true);
        refreshView.setAutoLoadMore(true);
        adapter.setCustomLoadMoreView(new XRefreshViewFooter(BuyBookDetailsActivity.this));
        refreshView.enableReleaseToLoadMore(true);
        refreshView.enableRecyclerViewPullUp(true);
        refreshView.enablePullUpWhenLoadCompleted(true);
        // 设置上次刷新的时间
        refreshView.restoreLastRefreshTime(lastRefreshTime);

        listUserChapterSubscribe(0);
    }

    public void listUserChapterSubscribe(int page) {
        post(Url.listUserChapterSubscribe, null, UserSubResponse.class, new FFNetWorkCallBack<UserSubResponse>() {
            @Override
            public void onSuccess(UserSubResponse response) {
                lastRefreshTime = refreshView.getLastRefreshTime();
                if (response.getCode() == 200) {
                    if (response.getData() == null || response.getData().size() < 20) {
                        refreshView.setLoadComplete(true);
                    }
                    if (page == 0) {
                        refreshView.stopRefresh();
                        refreshView.setLoadComplete(false);
                        adapter.setData(response.getData());
                    } else {
                        refreshView.stopLoadMore();
                        adapter.addData(response.getData());
                    }
                }
            }

            @Override
            public boolean onFail(UserSubResponse response) {
                return false;
            }
        }, "{\"offset\":" + page + ",\"limit\":20,\"bookId\":\"" + bookId + "\"}");
    }

    public class BuyBookAdapter extends BaseRecyclerAdapter<BuyBookViewHolder> {
        private Context context;


        private List<UserSubResponse.DataBean> mData = new ArrayList<>();


        public BuyBookAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<UserSubResponse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<UserSubResponse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public BuyBookViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.buybookdetails_item, parent, false);
            BuyBookViewHolder viewHolder_text = new BuyBookViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public BuyBookViewHolder getViewHolder(View view) {
            return new BuyBookViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @Override
        public void onBindViewHolder(final BuyBookViewHolder holder, @SuppressLint("RecyclerView") final int position, boolean isItem) {
            holder.tv_name.setText(mData.get(position).getChapterName());
            holder.tv_time.setText(FFUtils.stringToDates(mData.get(position).getTime()));
            holder.tv_price.setText(mData.get(position).getAmount()+"完结币");
        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }

    }
}
